package com.doctor.windflower_doctor.activity;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.entity.Msg;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.pop.VersionUpdatePop;
import com.doctor.windflower_doctor.util.Constent;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

/**
 * <p>
 * 功能 关于我们
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年10月29日 下午1:17:09
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class AboutUsActivity extends BaseActivity implements OnClickListener, Constent {
	private LinearLayout update_layout;
	private TextView textView, versions_name;
	private ImageButton back_btn;
	private WebView about_us_web;
	private WebSettings mWebSettings;
	final String mimeType = "text/html";
	final String encoding = "utf-8";
	private PackageManager packageManager;
	private PackageInfo packInfo = null;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {

	}

	@Override
	protected void setListener() {
		update_layout.setOnClickListener(this);
		back_btn.setOnClickListener(this);
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void findViews() {
		packageManager = getPackageManager();
		try {
			packInfo = packageManager.getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e1) {
			e1.printStackTrace();
		}
		textView = (TextView) findViewById(R.id.textView);
		versions_name = (TextView) findViewById(R.id.versions_name);
		textView.setText("关于我们");
		versions_name.setText("V" + packInfo.versionName);
		back_btn = (ImageButton) findViewById(R.id.back_btn);
		update_layout = (LinearLayout) findViewById(R.id.update_layout);
		about_us_web = (WebView) findViewById(R.id.about_us_web);
		mWebSettings = about_us_web.getSettings();
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setBuiltInZoomControls(false);
		mWebSettings.setLightTouchEnabled(true);
		mWebSettings.setSupportZoom(true);
		about_us_web.setHapticFeedbackEnabled(false);
		about_us_web.loadUrl("file:///android_asset/aboutus.html");
	}

	@Override
	public int getResLayoutId() {

		return R.layout.activity_about_us;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_layout:
			getVersion();
			break;
		case R.id.back_btn:
			finish();
			break;
		}

	}

	/**
	 * <p>
	 * 功能 获取升级版本
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月7日 下午3:57:22
	 */
	void getVersion() {
		HttpMethod requestMethod = HttpMethod.GET;
		// 获取packagemanager的实例
		String version = packInfo.versionCode + "";
		String requestUrl = URL_UPDATE + VERSIONS_UPDATE + "?" + APPSECRET + "=123123&appType=0&appKey=0&appVersion=" + version;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<DoctorAction> requestCallBack = new HttpRequestCallBack<DoctorAction>(new JsonParser(), DoctorAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<DoctorAction> responseInfo) {
				if (responseInfo.success.success) {
					deleteFilesByDirectory(SystemApplication.getInstance().file);
					LogUtils.e("=====handleResult=======" + responseInfo.success.data.downloadUrl);
					VersionUpdatePop versionUpdatePop = new VersionUpdatePop(AboutUsActivity.this, responseInfo.success.data.downloadUrl);
					versionUpdatePop.show();
				} else {
					Toast.makeText(AboutUsActivity.this, responseInfo.success.message, Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	private void deleteFilesByDirectory(File directory) {
		LogUtils.e("=========================开始删除=============1================");
		if (directory.isFile() && directory.exists()) {
			LogUtils.e("=========================开始删除===================2==========");
			directory.delete();
		}
	}
}
